/*
 * @(#)ActionResourceManager.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.resources;

import org.eoti.swing.DefaultAction;
import org.eoti.swing.text.Mnemonic;

import java.util.concurrent.*;
import java.awt.event.*;

public class ActionResourceManager
extends GUIResourceManager
{
	protected static final String RES_ACTION_CMD="cmd";
	protected static final String RES_ACTION_NAME="name";
	protected static final String RES_ACTION_SHORT_DESC="desc.short";
	protected static final String RES_ACTION_LONG_DESC="desc.long";
	protected static final String RES_ACTION_SMALL_ICON="icon.small";
	protected static final String RES_ACTION_LARGE_ICON="icon.large";
	protected static final String RES_ACTION_ACCEL="accel";

	protected static ConcurrentHashMap<String, DefaultAction> actions;
	protected GUIResourceManager parentManager;

	public ActionResourceManager(GUIResourceManager parentManager, String bundleName, String resourceKey, ActionListener listener)
	{
		super(bundleName, parentManager.getLocale());
		if(actions == null)
			actions = new ConcurrentHashMap<String, DefaultAction>();

		String[] keys = getStrings(resourceKey);
		for(String key : keys)
		{
			if( (key == null) || (key.length() == 0)) continue;
			DefaultAction action = getAction(key);
			action.addActionListener(listener);
		}
	}

	public static DefaultAction getLoadedAction(String prefix)
	{
		if(actions == null) return null;
		return actions.get(prefix);
	}

	/**
     * Constructs a new DefaultAction from the following keys:
	 * [prefix].cmd=copyCmd
	 * [prefix].name=&Copy...
     * the name has the ampersand(&amp;) before the letter it wishes to use as the mnemonic
	 * [prefix].desc.short=Copy
	 * [prefix].desc.long=Copy to the Clipboard
	 * [prefix].icon.small=/toolbarButtonGraphics/general/Copy16.gif
	 * [prefix].icon.large=/toolbarButtonGraphics/general/Copy24.gif
	 * [prefix].accel=ctrl C
	 * the accelerator key is as defined in
	 * http://java.sun.com/javase/6/docs/api/javax/swing/KeyStroke.html#getKeyStroke(java.lang.String)
	 *
	 * @param prefix to the keys in the resource
	 * @return DefaultAction constructed from the resource
	 */
	public DefaultAction getAction(String prefix)
	{
		DefaultAction action = getLoadedAction(prefix);
		if(action != null) return action;

		action = new DefaultAction();
		String name = getString(RES_FMT, prefix, RES_ACTION_NAME);
		if(!isDefined(name)) return null;
		action.setMnemonic(new Mnemonic(name));
		action.setActionCommand(getString(RES_FMT, prefix, RES_ACTION_CMD));
		action.setShortDescription(getString(RES_FMT, prefix, RES_ACTION_SHORT_DESC));
		action.setLongDescription(getString(RES_FMT, prefix, RES_ACTION_LONG_DESC));
		action.setSmallIcon(getString(RES_FMT, prefix, RES_ACTION_SMALL_ICON));
		action.setLargeIcon(getString(RES_FMT, prefix, RES_ACTION_LARGE_ICON));
		action.setAccelerator(getString(RES_FMT, prefix, RES_ACTION_ACCEL));
		actions.put(prefix, action);
		return action;
	}
}
